package shujuku.星期三180523课堂实践;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;


public class 查询人口 {
    public static void main(String[] args) throws Exception {
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("输入你的学号？");
//        String ID = scanner.nextLine();
//        int add = Integer.parseInt(String.valueOf(ID.charAt(0))) + Integer.parseInt(String.valueOf(ID.charAt(ID.length() - 1)));
//        if (add > 10) {
//            add = add - 10;
//        } else if (add == 10) {
//            add = 1;
//        }
//        String ID2 = add + ID.substring(1, ID.length() - 1);
//        System.out.println(ID2);
        String url = "jdbc:mysql://localhost:3306/world?" +
                "useUnicode=true&characterEncoding=UTF8&&user=root&&password=";
        Connection conn = (Connection) DriverManager.getConnection(url);
        Statement stmt = (Statement) conn.createStatement();
        try {

            String sql1 = "SELECT * FROM city WHERE city.Population>1017231";

            ResultSet rs1 = stmt.executeQuery(sql1);

            while (rs1.next()) {
                System.out.println(rs1.getInt(1) + "\t" + rs1.getString(2)+ "\t\t"+rs1.getString(3)
                +"\t\t"+ rs1.getString(4)+ "\t"+rs1.getString(5));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally{
            //7.关闭连接(顺序:后打开的先关闭)
            if(stmt!=null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
